<?php
include("dbInfo.php");

function getMembers () {
	$query = "SELECT * FROM accounts";
	$result = mysql_query($query);
	echo '<th>Name</th>><th>Birthday</th><th>Age</th><th>Gender</th><th>Account Type</th><th>Email Address</th>';
	
	while ( $row = mysql_fetch_array($result) ) {
	
		echo '
		<tr><td><a href="editMember.php?user='.$row['userID'].'">'.$row['name'].'</td>
		<td>'.$row['birthday'].'</td>
		<td>'.age($row['birthday']).'</td>
		<td>'.$row['gender'].'</td>		
		<td>'.$row['accountType'].'</td>
		<td>'.$row['email'].'</td>
		</tr> ';
		
	
	}
}

function memberDetails ($user) {
	$query = "SELECT * FROM accounts WHERE userID = $user";
	$result = mysql_query($query) or die("Could not get memberDetails for $user. " . mysql_error());
	
	while ( $row = mysql_fetch_array($result) ) {
	
		echo '
		<form action="index.php" method="POST">
		<input type="hidden" name="userID" id="userID" value="'.$user.'">
		<table>
		<tr><td>Name:</td><td><input type="text" name="name" id="name" value="'.$row['name'].'"></td></tr>
		<tr><td>Birthday:</td><td><input type="text" name="birthday" id="birthday" value="'.$row['birthday'].'"></td></tr>
		<tr><td>Gender:</td><td>
		<select name="gender" id="gender"> ';
		switch($row['gender']) {
			case 'Male' :
				echo '
				<option selected value="Male">Male</option>
				<option value="Female">Female</option>
				';
				break;
			case 'Female' :
				echo '
				<option value="Male">Male</option>
				<option selected value="Female">Female</option>
				';
				break;
		}
		echo '
		</select></td></tr>
		<tr><td>Account Type:</td><td>
		<select name="accountType" id="accountType"> ';
		switch($row['accountType']) {
			case 'swimmer' :
				echo '
				<option selected value="swimmer">swimmer</option>
				<option value="manager">manager</option>
				';
				break;
			case 'manager' :
				echo '
				<option value="swimmer">swimmer</option>
				<option selected value="manager">manager</option>
				';
				break;
		}
		echo '
		</select>
		</td></tr>
		<tr><td>Email:</td><td><input type="text" name="email" id="email" value="'.$row['email'].'"></td></tr>
		<tr><td><input type="submit" name="submit" value="Save Changes"></td></tr>
		</form>
		';
		
	
	}
}

function updateMember ($userID, $name, $accountType, $email, $gender, $birthday) {
	$query = "UPDATE accounts SET name = '$name', accountType = '$accountType', email = '$email', gender = '$gender', 				
			birthday = '$birthday'
			WHERE userID = '$userID'";
	mysql_query($query) or die("Could not update $name. " . mysql_error());
}

// Got from http://trevordavis.net/blog/php-get-age-function
function age($date) {
 
 $dates = explode("-", $date);
 $bYear = $dates[0];
 $bMonth = $dates[1];
 $bDay = $dates[2];
 
 $cMonth = date('n');
 $cDay = date('j');
 $cYear = date('Y');

 if(($cMonth >= $bMonth && $cDay >= $bDay) || ($cMonth > $bMonth)) {
  return ($cYear - $bYear);
 } else {
  return ($cYear - $bYear - 1);
 }
}  
?>
